<template>
  <a-dropdown>
    <div class="header-avatar" style="cursor: pointer">
      <a-avatar class="avatar" size="small" shape="circle" slot="avatar" src="../../assets/img/logo.png"/>
      <!-- <div><image src="../../assets/img/logo.png" style="width:24px;height: 24px;border-radius:50%" /></div> -->
      <span class="name">{{userName}}</span>
    </div>
    <a-menu :class="['avatar-menu']" slot="overlay">
      <!-- <a-menu-item @click="setting">
        <a-icon type="setting" />
        <span>设置</span>
      </a-menu-item> -->
      <a-menu-item @click="logout">
        <a-icon style="margin-right: 8px;" type="poweroff" />
        <span>退出登录</span>
      </a-menu-item>
    </a-menu>
  </a-dropdown>
</template>

<script>
import { mapGetters } from 'vuex'
import { logout } from '@/services/user'

export default {
  name: 'HeaderAvatar',
  data () {
    return {
      userName: ''
    }
  },
  computed: {
    ...mapGetters('account', ['user'])
  },
  created () {
    this.userName = localStorage.getItem('userName')
  },
  methods: {
    logout () {
      logout()
      this.$router.push('/login')
      this.$emit('logout')
    },
    setting () {
      this.$router.push('/setting')
    }
  }
}
</script>

<style lang="less">
  .header-avatar{
    display: inline-flex;
    .avatar, .name{
      align-self: center;
    }
    .avatar{
      margin-right: 8px;
    }
    .name{
      font-weight: 500;
    }
  }
  .avatar-menu{
    width: 150px;
    padding: 5px;
  }

</style>
